package com.sunny.ynhz.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.utils.AppManager;
import com.sunny.ynhz.utils.ShowUtils;

import butterknife.ButterKnife;
import rx.Subscription;

/**
 * 带titlebar 基类
 *
 * @author huhuanhuan
 * @version 1.0
 * @since 2016/5/19
 */
public abstract class AppBaseNoTranslationTitleBarActivity extends FragmentActivity {

    private TextView mTitleTextView;// 标题
    private TextView mBackwardbButton;// 左边返回
    private TextView mRightTextView;//右边文字
    private FrameLayout mContentLayout;// 主内容

    protected ImageView mImageRight;
    public SwipeRefreshLayout swipeRefreshLayout;
    private TextView rightTextView;
    private View title_root;
    private View find_rg;
    public View load_view;
    public Subscription subscription;
    private ImageView left_image;
    private View view;
    private TextView tv_load;
    private ProgressBar progress_view;
    private TextView tv_online;
    private TextView tv_look;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getInstance().addActivity(this);
        setupViews();
    }

    protected void showBackwardView(boolean show) {
        if (mBackwardbButton != null) {
            if (show) {
                mBackwardbButton.setVisibility(View.VISIBLE);
            } else {
                mBackwardbButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * @param
     */
    public void setLeftImage(int resourceId) {
        if (left_image != null) {
            mBackwardbButton.setVisibility(View.GONE);
            left_image.setImageResource(resourceId);
            left_image.setVisibility(View.VISIBLE);
            left_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onLeftImageClick();
                }
            });
        }// else ignored
    }

    private void onLeftImageClick() {
        finish();
    }

    public void doActivity(Class<?> c) {
        Intent intent = new Intent(this, c);
        startActivity(intent);

    }

    public int getTitleHigh() {
        return findViewById(R.id.rl_title).getMeasuredHeight();
    }

    protected void showRightView(String forwardResId, boolean show) {
        if (mRightTextView != null) {
            if (show) {
                mRightTextView.setVisibility(View.VISIBLE);
                mRightTextView.setText(forwardResId);
            } else {
                mRightTextView.setVisibility(View.INVISIBLE);
            }
        }// else ignored
    }

    protected void showRightView(int forwardResId, boolean show) {
        if (mRightTextView != null) {
            if (show) {
                mRightTextView.setVisibility(View.VISIBLE);
                mRightTextView.setBackgroundResource(forwardResId);
            } else {
                mRightTextView.setVisibility(View.INVISIBLE);
            }
        }
    }

    protected void setRightImage(int resid, boolean show) {
        if (mImageRight != null) {
            if (show) {
                mImageRight.setVisibility(View.VISIBLE);
                mImageRight.setImageResource(resid);
                mImageRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onclickRightImage();

                    }
                });
            } else {
                mImageRight.setVisibility(View.INVISIBLE);
            }
        }
    }

    public void onclickRightImage() {

    }

    /**
     * 隐藏刷新
     */
    protected void setHideRefreshView() {
        swipeRefreshLayout.setEnabled(false);
    }


    protected void onBackward(View backwardView) {
        finish();
    }

    protected void onRight(View forwardView) {
    }

    @Override
    public void setTitle(int titleId) {
        mTitleTextView.setText(titleId);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitleTextView.setVisibility(View.VISIBLE);
        title_root.setVisibility(View.VISIBLE);
        mTitleTextView.setText(title);
    }
    public void setTitleSize(int size) {
        mTitleTextView.setVisibility(View.VISIBLE);
        mTitleTextView.setTextSize(size);
    }
    public void setOnline(boolean isLine){
        if(isLine){
            tv_online.setVisibility(View.VISIBLE);
            tv_online.setText("在线");
        }
        else {
            tv_online.setVisibility(View.GONE);
        }
    }
    public void setLook(String msg){
        tv_look.setVisibility(View.VISIBLE);
        tv_look.setText(msg);
        tv_look.setTextColor(Color.WHITE);
    }
    public void hintTextString() {
        mTitleTextView.setVisibility(View.GONE);
        find_rg.setVisibility(View.VISIBLE);
    }
    @Override
    public void setTitleColor(int textColor) {
        mTitleTextView.setTextColor(textColor);
    }

    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        ButterKnife.bind(this);
        load_view = findViewById(R.id.root_load_view);
        load_view = findViewById(R.id.root_load_view);
        tv_online = (TextView)findViewById(R.id.tv_online);
        view = findViewById(R.id.root_load_view);
        tv_look = (TextView)findViewById(R.id.tv_look);

        onContentChanged();
        if (load_view != null) {
            tv_load = (TextView) findViewById(R.id.tv_load);
            progress_view = (ProgressBar) findViewById(R.id.pro_load);
            tv_load.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    resLoad();
                }
            });
        }
    }

    @Override
    public void setContentView(final View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }


    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }

    protected void onRightImage(View forwardView) {
    }
    public void setRightTextView(String right, String colorString) {
        rightTextView.setVisibility(View.VISIBLE);
        rightTextView.setTextSize(15);
        rightTextView.setText(right);
        if (colorString != null) {
            rightTextView.setTextColor(Color.parseColor(colorString));
        }
        rightTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickRightTextView();
            }
        });

    }
    public String getRightText() {
        String string = "";
        if (rightTextView != null) {
            string = rightTextView.getText().toString();
        }
        return string;
    }
    public void setRightTextViewSize(float size) {
        rightTextView.setVisibility(View.VISIBLE);
        rightTextView.setTextSize(size);
    }
    private void setupViews() {
        super.setContentView(R.layout.activity_title);// 获取主布局
        subscription = null;
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 全透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor("#2da7fd"));
        }
        title_root = findViewById(R.id.root_title);
        mContentLayout = (FrameLayout) findViewById(R.id.layout_content);//主布局
        mContentLayout.setBackgroundColor(Color.TRANSPARENT);
        mTitleTextView = (TextView) findViewById(R.id.text_title);// 获取标题
        rightTextView = (TextView) findViewById(R.id.title_ensure);
        mImageRight = (ImageView) findViewById(R.id.img_trash);
        mBackwardbButton = (TextView) findViewById(R.id.button_backward);//返回
        left_image = (ImageView) findViewById(R.id.left_image);//返回
        mBackwardbButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackward(v);
            }
        });
    }

    public void loadOutStart() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ShowUtils.disPlayProgress(AppBaseNoTranslationTitleBarActivity.this);
            }
        });

    }

    public void loadOutFail() {
        ShowUtils.closePopwindow();
    }

    public void loadOnAfter() {
        ShowUtils.closePopwindow();
    }


    public void setLeftTextView(String leftTextView) {
        mBackwardbButton.setTextSize(12);
        mBackwardbButton.setVisibility(View.VISIBLE);
        mBackwardbButton.setText(leftTextView);
    }

    public void closeKeyBoard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public void hideLeftBack() {
        mBackwardbButton.setVisibility(View.GONE);

    }

    public void hideTitle() {
        title_root.setVisibility(View.GONE);
    }

    public void showTitle() {
        title_root.setVisibility(View.VISIBLE);
    }

    public static void doAcitivity(Intent intent, Activity activity) {
        activity.startActivity(intent);
    }

    public void doAcitivity(Class tClass) {
        Intent intent = new Intent(this, tClass);
        startActivity(intent);
    }

    public void doAcitivity(Intent intent) {
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
        super.onDestroy();
    }

    public void resetSubscribed() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }

    public void clickRightTextView() {

    }


    //重新加载
    public void resLoad() {
        startLoad();

    }

    public void loadFail() {
        tv_load.setClickable(true);
        tv_load.setText("点击重新加载");
        progress_view.setVisibility(View.GONE);
    }

    public void loadSuccess() {
        view.setVisibility(View.GONE);
    }

    public void startLoad() {
        tv_load.setClickable(false);
        view.setVisibility(View.VISIBLE);
        tv_load.setText("正在加载");
        progress_view.setVisibility(View.VISIBLE);
    }

    public void loadEmply() {
        tv_load.setClickable(true);
        tv_load.setText("暂无数据");
        view.setVisibility(View.VISIBLE);
        progress_view.setVisibility(View.GONE);
    }
    /**
     * EditText获取焦点并显示软键盘
     */
    public void showSoftInputFromWindow(final Activity activity, final EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
//                activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.RESULT_SHOWN);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        },0);

    }
}
